//
//  QRCodelViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/3.
//

#import "QRCodelViewController.h"

@interface QRCodelViewController ()

@property (nonatomic, strong) UIImageView *bgImageV;
@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *qrImageV;

@end

@implementation QRCodelViewController

- (void)initDidLoad {
    self.navigationTitle = @"店铺二维码";
}
- (void)initViews {
    [self.view addSubview:self.bgImageV];
    [self.bgImageV addSubview:self.avatarImageV];
    [self.bgImageV addSubview:self.nameLabel];
    
    UILabel *tiplabel = [[UILabel alloc] init];
    tiplabel.text = @"如遇微信二维码扫不开，重新生成即可恢复访问";
    tiplabel.textColor = kBgColor;
    tiplabel.font = kFontRegula(11);
    [self.bgImageV addSubview:tiplabel];
    
    [self.bgImageV addSubview:self.qrImageV];
    
    UIView *labelBg = [[UIView alloc] init];
    labelBg.backgroundColor = kRGB(0xFFEADD);
    labelBg.layer.cornerRadius = 8;
    labelBg.layer.masksToBounds = YES;
    [self.bgImageV addSubview:labelBg];
    
    NSString *str = @"1. 尽量用APP扫一扫\n2. 微信如果扫不开用浏览器扫一扫\n3. 如果ma封了，在个人中心更新ma即可\n4. 永久网址是：https://www.xuanpingwang.com";
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:@"APP扫一扫"];
    NSRange range1 = [str rangeOfString:@"https://www.xuanpingwang.com"];
    [attr addAttributes:@{NSForegroundColorAttributeName:kBgColor} range:range];
    [attr addAttributes:@{NSForegroundColorAttributeName:kBgColor} range:range1];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = 6;
    paragraph.headIndent = 20;
    [attr addAttributes:@{NSParagraphStyleAttributeName:paragraph} range:NSMakeRange(0, str.length)];
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kRGB(0x666666);
    label.font = kFontMediium(12);
    label.numberOfLines = 0;
    label.attributedText = attr;
    [labelBg addSubview:label];
    
    UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [refreshBtn setImage:[UIImage imageNamed:@"icon_refreshMine"] forState:UIControlStateNormal];
    [refreshBtn setTitle:@"刷新二维码" forState:UIControlStateNormal];
    [refreshBtn setTitleColor:kBgColor forState:UIControlStateNormal];
    refreshBtn.titleLabel.font = kFontMediium(12);
    refreshBtn.layer.cornerRadius = 14;
    refreshBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.2);
    refreshBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    [self.view addSubview:refreshBtn];
    @weakify(self)
    [[refreshBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self getCodeData];
    }];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"保存二维码" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = kFontMediium(16);
    saveBtn.layer.cornerRadius = 20;
    saveBtn.backgroundColor = kBgColor;
    [self.view addSubview:saveBtn];
    [[saveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self saveImageWithView];
    }];
    
    [self.bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(347, 524));
        make.top.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
    }];
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(60);
        make.top.mas_equalTo(10);
        make.centerX.mas_equalTo(0);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.avatarImageV.mas_bottom).offset(8);
    }];
    [tiplabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10);
    }];
    [self.qrImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(200);
        make.top.mas_equalTo(tiplabel.mas_bottom).offset(38);
        make.centerX.mas_equalTo(0);
    }];
    [labelBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(295, 104));
        make.top.mas_equalTo(self.qrImageV.mas_bottom).offset(42);
        make.centerX.mas_equalTo(0);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(256, 80));
        make.center.mas_equalTo(0);
    }];
    [refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.bgImageV.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake(100, 28));
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(refreshBtn.mas_bottom).offset(28);
        make.size.mas_equalTo(CGSizeMake(230, 40));
    }];
}

- (void)initRequestData {
    [NET getUserInfo:kUserAccount.user.id success:^(id  _Nonnull responseObject) {
        UserModel *model = [UserModel mj_objectWithKeyValues:responseObject];
        [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
        self.nameLabel.text = model.nickName;
    } failure:^(NSError * _Nonnull error) {
    }];
    
    [self getCodeData];
}

- (void)getCodeData {
    [NET getQrcodeSuccess:^(id  _Nonnull responseObject) {
        NSString *codeUrl = responseObject[@"codeUrl"];
        [self.qrImageV sd_setImageWithURL:[NSURL URLWithString:codeUrl]];
    } failure:^(NSError * _Nonnull error) {
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

- (void)saveImageWithView {
    [YPProgressHud showHudInview:self.view];
    UIGraphicsBeginImageContextWithOptions(self.bgImageV.frame.size,NO,0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.bgImageV.layer renderInContext:ctx];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(saveImage:finishSaveWithError:context:), nil);
}

- (void)saveImage:(UIImage *)img finishSaveWithError:(NSError *)error context:(void*)context {
    if (error) {
        kShowText(@"保存失败,请稍后再试");
    }else {
        kShowText(@"保存成功");
    }
    [YPProgressHud hideHudForView:self.view];
}

- (UIImageView *)bgImageV {
    if (!_bgImageV) {
        _bgImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopqrbg"]];
        _bgImageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _bgImageV;
}
- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.layer.cornerRadius = 30;
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.clipsToBounds = YES;
    }
    return _avatarImageV;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = kFontMediium(14);
        _nameLabel.textColor = kRGB(0x333333);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}
- (UIImageView *)qrImageV {
    if (!_qrImageV) {
        _qrImageV = [[UIImageView alloc] init];
        _qrImageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _qrImageV;
}

@end
